﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AFCEPF.BankApplication.BLL;
using NUnit.Framework;


namespace AFCEPF.BankApplication.Tests
{
    /// <summary>    
    /// L'attribut [TestFixture] indique à NUnit
    /// que cette classe contient des tests..
    /// </summary>
    [TestFixture]
    class Test_AccountBase
    {
        private Customer _c;

        // indicates if the NegativeBalanceWithdraw event
        // has been fired.
        private bool _negativeBalanceWithdrawFired = false;

        // The two infos that the event will return..
        private Customer _eventCustomer;
        private double _eventBalance;


        [SetUp]
        public void SetUp()
        {
            _c = new Customer("toto");
        }

        [Test]
        public void Test_Ctor()
        {
            AccountBase account = Bank.Instance.Accounts.Create(_c, 1000);

            bool result = account.Holder == _c &&
                            account.Balance == 1000;

            Assert.IsTrue(result);
        }

        [Test]
        public void Test_Property_IsAccountExcessOverdrawn()
        {
            AccountBase account = Bank.Instance.Accounts.Create(_c, 1000);
            account.Withdraw(1001 + account.GetOverdrawnAmount());

            Assert.IsTrue(account.IsAccountExcessOverdrawn);
        }


        [Test]
        public void Test_Method_Deposit_ForPositiveAmount()
        {
            AccountBase account = Bank.Instance.Accounts.Create(_c, 1000);
            account.Deposit(1);

            Assert.IsTrue(account.Balance == 1001);
        }


        [Test]
        public void Test_Method_Deposit_ForNegativeAmount()
        {
            AccountBase account = Bank.Instance.Accounts.Create(_c, 1000);
            account.Deposit(-1);

            Assert.IsTrue(account.Balance == 1000);
        }


        [Test]
        public void Test_Method_Withdraw_ForPositiveAmount()
        {
            AccountBase account = Bank.Instance.Accounts.Create(_c, 1000);
            account.Withdraw(1);

            Assert.IsTrue(account.Balance == 999);
        }


        [Test]
        public void Test_Method_Withdraw_ForNegativeAmount()
        {
            AccountBase account = Bank.Instance.Accounts.Create(_c, 1000);
            account.Withdraw(-1);

            Assert.IsTrue(account.Balance == 1000);
        }




        [Test]
        public void Test_Event_NegativeBalanceWithdraw()
        {
            AccountBase account = Bank.Instance.Accounts.Create(_c,1000);


            //  let's subscribe to the event ..
            account.NegativeBalanceWithdraw += new EventHandler<AccountEventArgs>(OnNegativeBalanceWithdraw);
            account.Withdraw(2000);

            // Let's check that the NegativeBalanceWithdraw 
            // event has been correctly fired.
            bool result = _negativeBalanceWithdrawFired &&
                            (_eventBalance + (_eventBalance * 0.01)) == account.Balance &&
                            _eventCustomer == account.Holder;


            Assert.IsTrue(result);
        }

        void OnNegativeBalanceWithdraw(object sender, AccountEventArgs e)
        {

            _negativeBalanceWithdrawFired = true;
            _eventBalance = e.Balance;
            _eventCustomer = e.Holder;
        }






    }
}
